function DefaultAssistant() {
}

DefaultAssistant.prototype.trigger = function(name) {
	jQuery(this.controller.document).trigger(name);
};
DefaultAssistant.prototype.bind = function(name, fn) {
	jQuery(this.controller.document).bind(name, fn.bind(this));
};

DefaultAssistant.prototype.unbind = function(name, fn) {
	jQuery(this.controller.document).unbind(name, fn.bind(this));
};

DefaultAssistant.prototype.onConnection = function(state) {
	for(var st in  Strophe.Status) {
		if (Strophe.Status[st] === state) {
			console.log('state changed to: ' + st);
			jQuery(this.controller.document).find("#status").text(st);
			this.trigger(st);
		}
	}
};


DefaultAssistant.prototype.triggerRegistration = function() {
	console.log('triggerRegistration');
	var t = '' + new Date().getTime();
	var iq = $iq({type: 'set', id: t}).c('query',{xmlns: 'jabber:iq:register'});
	iq.c('username').t(t).up().c('password').t(t);
	console.log(iq.toString());
	// shame that we dont get a msg back from the server for that one ..
	// psi seems ok, maybe try with get first within same session?
	xxx.conn.send(iq);
	xxx.setToken(t);

	/// and shame, we have to use crude method to unbind
	// unbinding single event listener did not seem to work :(
	jQuery(this.controller.document).unbind();
	setTimeout(function() { xxx.conn.disconnect(); }, 300);
	this.bind('CONNECTED', this.addAdmin);
	setTimeout(function() { this.connect(); }.bind(this), 2000);
};

DefaultAssistant.prototype.addAdmin = function() {
	xxx.conn.send($pres({to: xxx.admin, type: 'subscribe'}));
	xxx.conn.addHandler(function(s) { 
		console.log('pres subsription from: ' + jQuery(s).attr('from'));
		xxx.conn.send($pres({type: 'subscribed', to: xxx.admin}));
		setTimeout(function() { 
			var txt = Strophe.getBareJidFromJid(xxx.conn.jid);
			xxx.conn.send($msg({to: xxx.admin, type: 'chat'}).c('body').t(txt));
		}, 400);
	}, null, 'presence', 'subscribe');
	jQuery(this.controller.document).unbind();
};

DefaultAssistant.prototype.sendPresence = function() {
	console.log('sending presence');
	xxx.conn.send($pres());
};

DefaultAssistant.prototype.connect = function() {
	console.log(xxx.url);
	console.log(xxx.jid + "/" + xxx.pw);
	jQuery(this.controller.document).find("#url").text(xxx.url);
	jQuery(this.controller.document).find("#creds").text(xxx.jid);
	xxx.conn = new Strophe.Connection(xxx.url);
	xxx.conn.connect(xxx.jid, xxx.pw, this.onConnection.bind(this));

	this.bind('CONNECTED', this.sendPresence);
	this.bind('CONNECTED', this.addHandlers.bind(this));
};

DefaultAssistant.prototype.addHandlers = function(fn) {
	console.log('adding handlers');
	var controller = this.controller;
	xxx.conn.addHandler(function(s) { 
		var txt = jQuery(s).text();
		txt = jQuery.trim(txt);
		console.log('msg: ' + txt);
		if (/^http/.test(txt)) {
			console.log('starting browser');
			controller.serviceRequest('palm://com.palm.applicationManager', {
				method: 'open',
				parameters: {
					target: txt
				}
			});
		}
		
		return true; 
	}, null, 'message', 'chat');
   
//	xxx.conn.addHandler(function(s) { 
//		console.log('pres: ' + jQuery(s).text());  
//		return true; 
//	}, null, 'presence');
//
};


DefaultAssistant.prototype.setup = function() {
};

DefaultAssistant.prototype.activate = function(event) {
	/* put in event handlers here that should only be in effect when this scene is active. For
	 example, key handlers that are observing the document */

	if (!xxx.hasToken()) {
		this.bind('CONNECTED', this.triggerRegistration);
	} else {
		this.bind('CONNECTED', function() { console.log('ready to service'); });
	}

	this.connect();
//	xxx.conn.xmlInput = function(body) {
//		var string = (new XMLSerializer()).serializeToString(body);
//		console.log(string);
//	};
};

DefaultAssistant.prototype.deactivate = function(event) {
	/* remove any event handlers you added in activate and do any other cleanup that should happen before
	 this scene is popped or another scene is pushed on top */
	jQuery(this.controller.document).unbind();

};

DefaultAssistant.prototype.cleanup = function(event) {
	/* this function should do any cleanup needed before the scene is destroyed as 
	 a result of being popped off the scene stack */

	/// race condition
	/// scene is destroyed (including connection) before we can disconnect
	xxx.conn.send($pres({type: 'unavailable'}));
	console.log('disconnecting ..');
	xxx.conn.disconnect();
};
